package com.yusiji.sqlSession;

import com.yusiji.pojo.Configuration;
import com.yusiji.pojo.MapperStatement;

import java.lang.reflect.*;
import java.util.List;
import java.util.Map;

/**
 * @author yusiji
 */
public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        List<Object> query = simpleExecutor.query(configuration, configuration.getMapperStatementMap().get(statementId), params);
        return (List<E>) query;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objects = selectList(statementId, params);

        if (objects != null && objects.size() == 1) {
            return (T) objects.get(0);
        } else {
            throw new RuntimeException("查询结果为空或者查询结果过多");
        }
    }

    @Override
    public Integer update(String statementId, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        Integer count = simpleExecutor.update(configuration, configuration.getMapperStatementMap().get(statementId), params);
        return count;
    }

    @Override
    public Integer insert(String statementId, Object... params) throws Exception {

        return update(statementId, params);
    }

    @Override
    public Integer delete(String statementId, Object... params) throws Exception {

        return update(statementId, params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {

        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className + "." + methodName;
                Map<String, MapperStatement> mapperStatementMap = configuration.getMapperStatementMap();
                MapperStatement mapperStatement = mapperStatementMap.get(statementId);
                String sqlType = mapperStatement.getSqlType();
                if ("select".equals(sqlType)) {
                    Type genericReturnType = method.getGenericReturnType();
                    if (genericReturnType instanceof ParameterizedType) {
                        List<Object> objects = selectList(statementId, args);
                        return objects;
                    }
                    return selectOne(statementId, args);
                } else if ("update".equals(sqlType)) {
                    return update(statementId, args);
                } else if ("insert".equals(sqlType)) {
                    return insert(statementId, args);
                } else if ("delete".equals(sqlType)) {
                    return delete(statementId, args);
                }else{
                    throw new RuntimeException("sql 标签不符合要求，请检查xml");
                }
            }
        });
        return (T) proxyInstance;
    }
}
